package main

import (
	"errors"
	"log"
	"time"

	"github.com/go-sql-driver/mysql"
	_ "github.com/go-sql-driver/mysql"
	"github.com/jinzhu/gorm"
)

var gOrm *gorm.DB

type Motor struct {
	Id         int32     `json:"id" gorm:"column:id" sql:"id"`
	ConfigId   int32     `json:"config_id" gorm:"column:config_id" sql:"config_id"`
	Sn         string    `json:"sn" gorm:"column:sn" sql:"sn"`
	DriverSn   string    `json:"driver_sn" gorm:"column:driver_sn" sql:"driver_sn"`
	ConfigTime time.Time `json:"config_time" gorm:"column:config_time" sql:"config_time"`
	ModelId    int32     `json:"model_id" gorm:"column:model_id" sql:"model_id"`
	ConfigName string    `json:"config_name" gorm:"column:config_name" sql:"config_name"`
	DeleteAt   time.Time `json:"delete_at" gorm:"column:delete_at" sql:"delete_at"`
}

func (m *Motor) TableName() string {
	return "motor"
}
func InitDB() error {
	// try to connect to mysql database.
	cfg := mysql.Config{
		User:                 "root",
		Passwd:               "password",
		Addr:                 "127.0.0.1", //IP:PORT
		Net:                  "tcp",
		DBName:               "manufacture",
		Loc:                  time.Local,
		AllowNativePasswords: true,
		ParseTime:            true,
	}
	str := cfg.FormatDSN()
	var e error

	gOrm, e = gorm.Open("mysql", str)
	if nil != e {
		log.Print(e.Error())
		return errors.New("connot connect mysql server")
	}
	return nil
}
